<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="活动名称" prop="name">
        <el-input placeholder="活动名称" v-model="dataForm.name" maxlength="15"></el-input>
      </el-form-item>
      <el-form-item label="起止时间" prop="start_time" class="date-lable">
        <el-form-item prop="start_time" label-width="0px">
          <el-date-picker v-model="dataForm.start_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <span style="padding: 0 20px">——</span>
        <el-form-item prop="end_time" label-width="0px">
          <el-date-picker v-model="dataForm.end_time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item label="周期" prop="cycle_mode">
        <el-radio-group v-model="dataForm.cycle_mode">
          <el-radio :label="0">每天</el-radio>
          <el-radio :label="1">每周</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="dataForm.cycle_mode == '1'">
        <el-checkbox-group v-model="dataForm.week_list" @change="selectWeek">
          <el-checkbox-button v-for="item in weeklist" :label="item.label" :key="item.label">{{ item.name }}</el-checkbox-button>
        </el-checkbox-group>
      </el-form-item>

      <div class="mt10">
        <el-form-item label="时间段" prop="start_hour">
          <el-radio-group v-model="timeQuantum" @change="selectTimeQuantum">
            <div class="mt30">
              <el-radio :label="1"> 9:00-12:00</el-radio>
              <el-radio :label="6"> 12:00-13:00</el-radio>
            </div>
            <div class="mt30">
              <el-radio :label="2">13:00-15:00</el-radio>
              <el-radio :label="7">15:00-16:00</el-radio>
            </div>
            <div class="mt30">
              <el-radio :label="3">16:00-18:00</el-radio>
              <el-radio :label="8">18:00-19:00</el-radio>
            </div>
            <div class="mt30">
              <el-radio :label="4">19:00-21:00</el-radio>
              <el-radio :label="9">21:00-22:00</el-radio>
            </div>
            <div class="mt30">
              <el-radio :label="5">22:00-23:59</el-radio>
              <el-radio :label="10">0:00-9:00</el-radio>
            </div>
          </el-radio-group>
        </el-form-item>
      </div>

      <el-form-item label="活动图片" prop="img_url">
        <Imglist :limit="1" @updataImg="updataImg" :value="imgArr" :dialogVisible="visible" :tip="'* 尺寸建议750x268'"></Imglist>
        <!-- <el-dialog :visible.sync="dialogVisible">
          <img width="750px" height="268px" :src="dataForm.img_url" alt="" />
        </el-dialog> -->
      </el-form-item>
      <el-form-item label="活动说明" prop="explain">
        <el-input type="textarea" placeholder="" v-model="dataForm.explain" :maxlength="200"></el-input>
      </el-form-item>
      <el-form-item label="订单自动关闭时间(分钟)" prop="close_time">
        <el-col :span="4">
          <el-input-number v-model="dataForm.close_time" :min="3" :precision="0" :controls="false" :max="360"></el-input-number>
        </el-col>
        <el-col :span="12" :offset="3">可输入3-360分钟</el-col>

      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import seviceApi from '@/api/serviceApi.js'
import Imglist from '../../oss/oss-uploadImg.vue'
import commissionCenterListVue from '../product/commissionCenterList.vue'
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      value: 1,
      week: '', // 选择星期
      timeQuantum: '', // 选择时间段
      dialogVisible: false,
      dataForm: {},
      imgArr: [],
      weeklist: [
        {
          label: 1,
          name: '周一'
        },
        {
          label: 2,
          name: '周二'
        },
        {
          label: 3,
          name: '周三'
        },
        {
          label: 4,
          name: '周四'
        },
        {
          label: 5,
          name: '周五'
        },
        {
          label: 6,
          name: '周六'
        },
        {
          label: 0,
          name: '周日'
        }
      ],
      dataRule: {
        name: [
          {
            required: true,
            message: '菜单名称不能为空',
            trigger: 'blur'
          }
        ],
        start_time: [
          {
            required: true,
            message: '起止时间不能为空',
            trigger: 'blur'
          }
        ],
        end_time: [
          {
            required: true,
            message: '起止时间不能为空',
            trigger: 'blur'
          }
        ],
        img_url: [
          {
            required: true,
            message: '活动图片不能为空',
            trigger: 'blur'
          }
        ],
        close_time: [
          {
            required: true,
            message: '订单自动关闭时间(小时)不能为空',
            trigger: 'blur'
          }
        ],
        start_hour: [
          {
            required: true,
            message: '时间段不能为空',
            trigger: 'blur'
          }
        ],
        cycle_mode: [
          {
            required: true,
            message: '周期不能为空',
            trigger: 'change'
          }
        ]
      },
      goods_category_list: []
    }
  },

  methods: {
    init (id) {
      this.resetData()
      this.dataForm.id = id || null
      this.visible = true

      if (id != undefined) {
        seviceApi.getSeckillactivityInfo(id).then((res) => {
          debugger
          if (res && res.data.code == 200) {
            if (res.data.data.start_hour == '9:00') {
              this.timeQuantum = 1
            } else if (res.data.data.start_hour == '13:00') {
              this.timeQuantum = 2
            } else if (res.data.data.start_hour == '16:00') {
              this.timeQuantum = 3
            } else if (res.data.data.start_hour == '19:00') {
              this.timeQuantum = 4
            } else if (res.data.data.start_hour == '22:00') {
              this.timeQuantum = 5
            } else if (res.data.data.start_hour == '12:00') {
              this.timeQuantum = 6
            } else if (res.data.data.start_hour == '15:00') {
              this.timeQuantum = 7
            } else if (res.data.data.start_hour == '18:00') {
              this.timeQuantum = 8
            } else if (res.data.data.start_hour == '21:00') {
              this.timeQuantum = 9
            } else if (res.data.data.start_hour == '0:00') {
              this.timeQuantum = 10
            }
            this.dataForm = res.data.data

            this.imgArr = this.dataForm.img_url.split(';')
            console.log(this.imgArr)
          }
        })
      }
    },
    /** 重置表单数据 */
    resetData () {
      this.dataForm = {
        name: '',
        start_hour: '',
        end_hour: '',
        start_time: '',
        end_time: '',
        img_url: '',
        explain: '',
        close_time: '',
        cycle_mode: '',
        week_list: []
      }
      this.resetForm('dataForm')
      this.imgArr = []
      this.timeQuantum = ''
    },
    // 上传图片
    updataImg (e) {
      this.dataForm.img_url = e
      this.$refs['dataForm'].clearValidate(['img_url'])
    },
    // 选择周几
    selectWeek () {
      // console.log(this.dataForm.week_list)
    },
    // 选择时间段
    selectTimeQuantum () {
      debugger
      if (this.timeQuantum == 1) {
        this.dataForm.start_hour = '9:00'
        this.dataForm.end_hour = '12:00'
      } else if (this.timeQuantum == 2) {
        this.dataForm.start_hour = '13:00'
        this.dataForm.end_hour = '15:00'
      } else if (this.timeQuantum == 3) {
        this.dataForm.start_hour = '16:00'
        this.dataForm.end_hour = '18:00'
      } else if (this.timeQuantum == 4) {
        this.dataForm.start_hour = '19:00'
        this.dataForm.end_hour = '21:00'
      } else if (this.timeQuantum == 5) {
        this.dataForm.start_hour = '22:00'
        this.dataForm.end_hour = '23:59'
      } else if (this.timeQuantum == 6) {
        this.dataForm.start_hour = '12:00'
        this.dataForm.end_hour = '13:00'
      } else if (this.timeQuantum == 7) {
        this.dataForm.start_hour = '15:00'
        this.dataForm.end_hour = '16:00'
      } else if (this.timeQuantum == 8) {
        this.dataForm.start_hour = '18:00'
        this.dataForm.end_hour = '19:00'
      } else if (this.timeQuantum == 9) {
        this.dataForm.start_hour = '21:00'
        this.dataForm.end_hour = '22:00'
      } else if (this.timeQuantum == 10) {
        this.dataForm.start_hour = '0:00'
        this.dataForm.end_hour = '9:00'
      }
      this.$refs['dataForm'].clearValidate(['start_hour'])
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (
            Object.prototype.toString.call(this.dataForm.img_url) ===
            '[object Array]'
          ) {
            this.dataForm.img_url = this.dataForm.img_url.join(',')
          } else {
            this.dataForm.img_url = this.dataForm.img_url
          }
          if (new Date(this.dataForm.end_time).getTime() - new Date(this.dataForm.start_time).getTime() < 0) {
            this.$message({
              showClose: true,
              message: '结束时间不能小于开始时间',
              type: 'error'
            })
            return false
          }
          if (this.dataForm.id == null || undefined) {
            delete (this.dataForm.id)
            debugger
            seviceApi.saveSeckillactivity(this.dataForm).then((res) => {
              if (res && res.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          } else {
            delete this.dataForm.discard
            seviceApi.updateSeckillactivity(this.dataForm).then((res) => {
              if (res && res.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-radio__label {
  border: 1px solid #dcdfe6;
  padding: 8px 12px;
  margin-left: 10px;
  border-radius: 4px;
}

.mt10 {
  margin-top: 10px;
}

.mt30 {
  margin-top: 30px;
}
.date-lable /deep/ .el-form-item__content {
  display: flex;
  justify-content: flex-start;
}
</style>
